<?
require_once('../lib/Config.php');
require_once('../lib/DB.gear.php');
require_once('../lib/lib.gear.php');
require_once('../lib/lib.garden.php');
require_once('../lib/oauth.class.php');
require_once('../lib/people.class.php');
require_once('../lib/signup.class.php');

if(isset($_POST['g_key_din39s03j23ogn']) && $_POST['g_key_din39s03j23ogn'] == G_KEY)
{
    $user_id   = $_POST['user_id'];
    $mixi_id   = $_POST['mixi_id'];
    $id_type   = $_POST['id_type'];
 	$type      = $_POST['type'];
 	$mixi_josn = $_POST['mixi_json'];

	switch($type)
	{
		case 'is_user':
			$signup = new SIGNUP;

			if($signup->load_user($user_id, $id_type))
			{
			    $user_id       = $signup->get_user_id();
			    $mixi_id       = $signup->get_mixi_id();
			    $facebook_id   = $signup->get_facebook_id();
			    $twitter_id    = $signup->get_twitter_id();
			    $user_name     = $signup->get_user_name();
			    $profile_image = $signup->get_profile_image();

			    $data = array(
			                   'user_id'        => $user_id,
			                   'mixi_id'        => $mixi_id,
			                   'facebook_id'    => $facebook_id,
			                   'twitter_id'     => $twitter_id,
			                   'user_name'      => $user_name,
			                   'profile_image'  => $profile_image,
			                   'status'         => 'true',
			                  );
			}
			else
			{
				$data['status'] = 'false';
			}

		    $json_data = json_encode($data);
		    echo $json_data;
		break;

		case 'add_friends':
		 			echo 'Before';
			$jsonArray = json_decode($mixi_josn, true);
			foreach($jsonArray['entry'] as $parson)
			{
			    add_mixi_list($user_id, $parson['id'], $parson['displayName'], $parson['thumbnailUrl']);
			}
 			echo 'OK';
		break;

		case 'update_using':
		    $db = new DB_GEAR();

		    //set query
		    $aQuery['Type']   = 'update';
		    $aQuery['Column'] = array(
		    						  'using_flag' => $user_id,
		    						  );
		    $aQuery['Table']  = 'garden_mixi_tbl';
		    $aQuery['Where']  = 'mixi_id = "'.$mixi_id.'"';

		    //set query
		    $db->set_query($aQuery);

			//echo $db->get_query();

		    //send query
		    $result = $db->send_query(1);
		break;

		case 'get_using_user':
		    $db = new DB_GEAR();

		    //set query
		    $aQuery['Type']   = 'direct';
		    $aQuery['Query']  = 'select user.* from garden_user_tbl as user inner join garden_mixi_tbl as mixi on user.user_id = mixi.using_flag where mixi.user_id = '.$user_id;

		    //set query
		    $db->set_query($aQuery);
		    //echo $db->get_query();
		    //send query
		    $result = $db->send_query();
		    
			if(mysql_num_rows($result))
			{
				while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
				{
					$aList[] = $rec;
				}
			}
			else
			{
			    $data = array(
			                   'status'  => 'error',
			                  );
			    $json_data = json_encode($data);
			}

		    $json_data = json_encode($aList);

		    echo $json_data;
		break;

		case 'post':
		break;

	}

}
else
{
    $data = array(
                   'status'  => 'error',
                  );
    $json_data = json_encode($data);
}

?>
